<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/1/24
 * Time: 15:43
 */

namespace app\admin\logic;


use think\Db;
use think\Exception;

class NoteLogic
{
    /**
     * 添加签
     * @param $note
     * @param $type_ids
     * @param $content_list
     * @param $solve_content_list
     * @return array
     */
    public function addNote($note,$type_ids,$content_list,$solve_content_list){
        Db::startTrans();
        try{
//            $note['solve_note_ids'] = implode(',',$solve_note_ids);
            $result = $this->check($type_ids,$solve_content_list);
            if($result['status'] != 1){
                return $result;
            }
            $solve_note_list = $result['solve_note_list'];
            $solve_note_ids = '';
            foreach ($solve_note_list as $key=>$value){
                $solve_note = model('SolveNote')->create($value);
                $solve_note_id = $solve_note['id'];
                if(empty($solve_note_ids)) {
                    $solve_note_ids .= $solve_note_id;
                }else{
                    $solve_note_ids .= ','.$solve_note_id;
                }
            }
            $note['solve_note_ids'] = $solve_note_ids;
            $note_obj = model('Note')->create($note);
            foreach ($content_list as $key=>$value){
                $content['content'] = $value;
                $content['note_id'] = $note_obj['id'];
                Db::name('Content')->insert($content);
            }
            Db::commit();
            return array('status'=>1);
        }catch (Exception $e){
            Db::rollback();
            return array('status'=>0,'error'=>$e->getMessage());
        }
    }

    private function check($type_ids,$solve_content_list){
        if(!isset($type_ids) || !isset($solve_content_list) || count($type_ids) ==0 || count($solve_content_list) == 0){
            return array('status'=>2,'msg'=>'请添加解签！');
        }
        if(count($solve_content_list) != count($type_ids)){
            return array('status'=>2,'msg'=>'解签内容与解签类型个数不一致！');
        }
        $solve_note_list = [];
        foreach ($type_ids as $key=>$value){
            if(empty($value)){
                return array('status'=>2,'msg'=>'请选择解签类型！');
            }
            $solve_note = [];
            $solve_note['type_id'] = $value;
            $solve_note['content'] = '';
            $solve_note_list[] = $solve_note;
        }
        foreach ($solve_content_list as $key=>$value){
//            if(empty($value)){
//                return array('status'=>2,'msg'=>'解签内容不能为空！');
//            }
            $solve_note_list[$key]['content'] = $value;
        }
        return array('status'=>1,'solve_note_list'=>$solve_note_list);
    }

    /**
     * 编辑签
     * @param $note
     * @param $type_ids
     * @param $content_list
     * @param $solve_content_list
     * @return array
     */
    public function editNote($note,$type_ids,$content_list,$solve_content_list){
        Db::startTrans();
        try{
//            $note['solve_note_ids'] = implode(',',$solve_note_ids);
            $result = $this->check($type_ids,$solve_content_list);
            if($result['status'] != 1){
                return $result;
            }
            $solve_note_list = $result['solve_note_list'];
            $solve_note_ids = model('Note')->where(array('id'=>$note['id']))->value('solve_note_ids');
            $solve_note_ids_arr = explode(',',$solve_note_ids);
            if(count($solve_note_ids_arr) > 0) {
                Db::name('SolveNote')
                    ->where('id', 'in', $solve_note_ids_arr)
                    ->delete();
            }
            $solve_note_ids = '';

            foreach ($solve_note_list as $key=>$value){
                $solve_note = model('SolveNote')->create($value);
                $solve_note_id = $solve_note['id'];
                if(empty($solve_note_ids)) {
                    $solve_note_ids = $solve_note_id;
                }else{
                    $solve_note_ids .= ','.$solve_note_id;
                }
            }
            $note['solve_note_ids'] = $solve_note_ids;
            model('Note')
                ->where(array('id'=>$note['id']))
                ->update($note);
            Db::name('Content')
                ->where(array('note_id'=>$note['id']))
                ->delete();
            foreach ($content_list as $key=>$value){
                $content['content'] = $value;
                $content['note_id'] = $note['id'];
                Db::name('Content')->insert($content);
            }
            Db::commit();
            return array('status'=>1);
        }catch (Exception $e){
            Db::rollback();
            return array('status'=>0);
        }
    }
}